"use strict";

const Controller = require("egg").Controller;

const dayjs = require("dayjs");

class RegisterController extends Controller {
  async index() {
    const { ctx, app } = this;

    const { username, password } = ctx.request.body;

    const data = await app.mysql.get("user", { username });

    if (!data) {
      await app.mysql.insert("user", {
        username,
        password,
        createtime: dayjs(new Date().getTime()).format("YYYY-MM-DD HH:mm:ss"),
      });

      ctx.body = {
        code: 1,
        msg: "注册成功",
      };
    } else {
      ctx.status = 401;

      ctx.body = {
        code: 0,
        msg: "该用户已被注册",
      };
    }
  }
}

module.exports = RegisterController;
